<template>
  <div class="department">
    <el-tabs v-model="activeName" @tab-click="">
      <el-tab-pane label="组织架构" name="first"></el-tab-pane>
    </el-tabs>
    <div class="bigbox row-padd">
      <el-row type="flex">
        <el-col
          ><i class="el-icon-platform-eleme"></i
          ><span class="title">{{headlist.name}}</span> </span></el-col
        >
        <el-col :span="4">
          <el-row type="flex">
            <el-col class="fuze">负责人</el-col>
            <el-col>
              <el-dropdown>
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item  @click='sclick' >添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="box"></div>
          <el-tree :data="list" default-expand-all>
            <treetools
              slot-scope="{ data }"
              :data="data"
               @updatedata="getAllList"
               @opendialog='sclick' 
            ></treetools>
          </el-tree>
    </div>
    <adddept :digshow='isshow' @closedig='closclick'
    ref="minefrom"
     :currentItem='currentItem' :type='type' @updatedata="getAllList"></adddept>
  </div>
</template>

<script>
import { geteverylist } from "../../api/departments";
export default {
  components: {
    treetools: () => import("./tree-tools.vue"), // 树操作栏组件
    adddept: () => import("./add.vue"), // 新增组件
  },
  data() {
    return {
      activeName: "first",
      list: [],
      headlist: [],
      isshow: false,
      currentItem: {},
      type: "",
    };
  },
  methods: {
    sclick(item) {
      this.isshow = true;
      this.currentItem = item.data;
      this.type = item.process;
      if (this.type === "eait") {
        this.$refs.minefrom.form = { ...this.currentItem };
      }
    },
    closclick() {
      this.isshow = false;
    },
    async getAllList() {
      let res = await geteverylist();
      // console.log(res);
      this.list = res.depts;
      this.headlist = this.list[0];
      function plan(arr, pid) {
        let res1 = [];
        arr.forEach((item) => {
          if (item.pid === pid) {
            var children = plan(arr, item.id);
            if (children.length > 0) {
              item.children = children;
            }
            res1.push(item);
          }
        });
        return res1;
      }
      // console.log(plan(this.list, ""));
      this.list = plan(this.list, "");
    },
  },
  created() {
    this.getAllList();
  },
};
</script>

<style lang='scss'>
.department {
  padding: 20px 20px 20px 50px;
  background-color: white;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  border: 1px solid #ebeef5;
  .title {
    font-weight: bold;
  }
  .row-padd {
    padding: 30px 70px 5px 70px;
  }
  .fuze {
    margin-right: 15px;
    font-size: 14px;
    margin-top: 2px;
  }
  .box {
    margin-bottom: 10px;
    padding: 5px 0;
    height: 1px;
    border: 1px solid transparent;
    border-bottom: 1px solid rgba(231, 231, 231);
  }
}
</style>